﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Lab3.Entities
{
    class Skybox : Entity
    {
        private float _skyboxSize = 500f;
        private Effect _skyboxEffect;
        private TextureCube _skyboxTexture;
        private GraphicsDevice _device;

        public Skybox(Camera camera, Effect effect, TextureCube textureCube, GraphicsDevice device) : base(camera)
        {
            _skyboxEffect = effect;
            _skyboxTexture = textureCube;
            _device = device;
        }

        public override void Draw(int type)
        {
            _device.RasterizerState = RasterizerState.CullClockwise;
            foreach (EffectPass pass in _skyboxEffect.CurrentTechnique.Passes)
            {
                foreach (ModelMesh mesh in Model.Meshes)
                {
                    foreach (ModelMeshPart part in mesh.MeshParts)
                    {
                        part.Effect = _skyboxEffect;
                        part.Effect.Parameters["World"].SetValue(Matrix.CreateScale(_skyboxSize) * Matrix.CreateTranslation(Camera.Position));
                        part.Effect.Parameters["View"].SetValue(Camera.View);
                        part.Effect.Parameters["Projection"].SetValue(Camera.Projection);
                        part.Effect.Parameters["SkyBoxTexture"].SetValue(_skyboxTexture);
                        part.Effect.Parameters["CameraPosition"].SetValue(Camera.Position);
                    }
                    mesh.Draw();
                }
            }
            _device.RasterizerState = RasterizerState.CullCounterClockwise;
        }
    }
}
